package io.renren.modules.medical.controller;

import java.util.List;
import java.util.Map;
import io.renren.common.result.Result;
import io.renren.common.utils.PageUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.medical.entity.IllSubEntity;
import io.renren.modules.medical.entity.PatientBasicInformationEntity;
import io.renren.modules.medical.service.PatientBasicInformationService;
import io.renren.modules.medical.vo.medicalVo.InfoDeleteVo;
import io.renren.modules.medical.vo.medicalVo.PatientBasicDetailVo;
import io.renren.modules.medical.vo.medicalVo.PatientNameAndIdVo;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;


/**
 * 病人入组基本信息接口
 * @author luosheng
 * @date 2023-05-04 16:23:35
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("medical/patientBasicInfo")
public class PatientBasicInformationController {

    private final PatientBasicInformationService patientBasicInformationService;

    /**
     * 列表
     */
    @RequiresPermissions("basicinfo:list")
    @GetMapping("/list")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params){
        PageUtils page = patientBasicInformationService.queryPage(params);
        return Result.success(page);
    }

    /**
     * 根据basicId获取到病情子集
     */
    @RequiresPermissions("basicinfo:getsubsbybasicId")
    @GetMapping("/getSubsByBasicId/{patientBasicInfoId}")
    public Result<List<IllSubEntity>> getListByBisicId(@PathVariable("patientBasicInfoId") Long patientBasicInfoId){
        List<IllSubEntity> listResult = patientBasicInformationService.getSubsByBasicId(patientBasicInfoId);
        return Result.success(listResult);
    }

    /**
     * 信息录入模块根据subId拿到用户集合
     */
    @GetMapping("/patients/{subId}")
    public Result<List<PatientNameAndIdVo>> getBySubId(@PathVariable("subId") Long subId){
        List<PatientNameAndIdVo> patients = patientBasicInformationService.patients(subId);
        return Result.success(patients);
    }


    /**
     * 获取患者详情 根据id获取
     */
    @RequiresPermissions("basicinfo:detail")
    @GetMapping("/detail/{id}")
    public Result<PatientBasicDetailVo> getDetail(@PathVariable Long id){
        PatientBasicDetailVo patientBasicDetailVo = patientBasicInformationService.getDetail(id);
        return Result.success(patientBasicDetailVo);
    }

    /**
     * 信息
     */
    @RequiresPermissions("basicinfo:info")
    @GetMapping("/info/{id}")
    public Result<PatientBasicDetailVo> info(@PathVariable("id") Long id){
        PatientBasicDetailVo allInformation = patientBasicInformationService.getAllInformation(id);
        return Result.success(allInformation);
    }

    /**
     * 获取要删除的集合
     */
    @RequiresPermissions("basicinfo:deletelist")
    @GetMapping("/deleteList/{basicId}")
    public Result<List<InfoDeleteVo>> getDeleteList(@PathVariable("basicId") Long basicId){
        List<InfoDeleteVo> deleteList = patientBasicInformationService.getDeleteList(basicId);
        return Result.success(deleteList);
    }

    /**
     * 删除掉用户基础信息表
     */
    @RequiresPermissions("basicinfo:deletebasic")
    @PostMapping("deleteBasic/{basicId}")
    public Result<String> deleteBasic(@PathVariable("basicId") @NotNull Long basicId){
        patientBasicInformationService.deleteBasic(basicId);
        return Result.success(null);
    }

    /**
     * 保存
     */
    @RequiresPermissions("basicinfo:save")
    @PostMapping("/save")
    public Result<Long> save(@RequestBody @Validated(AddGroup.class) PatientBasicInformationEntity patientBasicInfo){
		Long basicInfoId = patientBasicInformationService.saveBasicInfo(patientBasicInfo);
        return Result.success(basicInfoId);
    }

    /**
     * 修改
     */
    @RequiresPermissions("basicinfo:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated(UpdateGroup.class) PatientBasicInformationEntity patientBasicInformation){
		patientBasicInformationService.updateById(patientBasicInformation);
        return Result.success(null);
    }

    /**
     * 删除
     */
    @RequiresPermissions("basicinfo:delete")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody InfoDeleteVo infoDeleteVo){
		patientBasicInformationService.removeInfo(infoDeleteVo);
        return Result.success(null);
    }

}
